import axios from 'axios';

// 使用我们的FastAPI服务
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8001';

const apiClient = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json'
  }
});

export const materialsProjectService = {
  // 搜索材料
  async searchMaterials(query, limit = 10) {
    try {
      const response = await apiClient.get('/materials/search', {
        params: { query, limit }
      });
      return response.data;
    } catch (error) {
      console.error('Error searching materials:', error);
      throw error;
    }
  },

  // 获取材料详情
  async getMaterialDetails(materialId) {
    try {
      const response = await apiClient.get(`/materials/${materialId}`);
      return response.data;
    } catch (error) {
      console.error('Error fetching material details:', error);
      throw error;
    }
  },

  // 获取晶体结构数据
  async getCrystalStructure(materialId) {
    try {
      const response = await apiClient.get(`/materials/${materialId}/structure`);
      return response.data;
    } catch (error) {
      console.error('Error fetching crystal structure:', error);
      throw error;
    }
  },

  // 获取电子结构数据
  async getElectronicStructure(materialId) {
    try {
      const response = await apiClient.get(`/materials/${materialId}/electronic`);
      return response.data;
    } catch (error) {
      console.error('Error fetching electronic structure:', error);
      throw error;
    }
  },

  // 获取示例材料
  async getExampleMaterials() {
    try {
      const response = await apiClient.get('/examples');
      return response.data;
    } catch (error) {
      console.error('Error fetching examples:', error);
      throw error;
    }
  },

  // 获取材料论文信息
  async getMaterialPapers(materialId, limit = 100) {
    try {
      const response = await apiClient.get(`/materials/${materialId}/papers`, {
        params: { limit }
      });
      return response.data;
    } catch (error) {
      console.error('Error fetching material papers:', error);
      throw error;
    }
  },

  // 获取材料价格信息
  async getMaterialPrices(materialId, limit = 50) {
    try {
      const response = await apiClient.get(`/materials/${materialId}/prices`, {
        params: { limit }
      });
      return response.data;
    } catch (error) {
      console.error('Error fetching material prices:', error);
      throw error;
    }
  },

  // 直接搜索论文
  async searchPapers(query, limit = 100) {
    try {
      const response = await apiClient.get('/search/baidu/papers', {
        params: { query, limit }
      });
      return response.data;
    } catch (error) {
      console.error('Error searching papers:', error);
      throw error;
    }
  },

  // 直接搜索价格
  async searchPrices(query, limit = 50) {
    try {
      const response = await apiClient.get('/search/baidu/prices', {
        params: { query, limit }
      });
      return response.data;
    } catch (error) {
      console.error('Error searching prices:', error);
      throw error;
    }
  },

  // 健康检查
  async healthCheck() {
    try {
      const response = await apiClient.get('/health');
      return response.data;
    } catch (error) {
      console.error('Health check failed:', error);
      throw error;
    }
  }
};

export default materialsProjectService;
